package com.dong.mcd.control;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.dong.mcd.model.SysUser;
import com.dong.mcd.param.RoleParam;
import com.dong.mcd.param.UserParam;
import com.dong.mcd.pub.BaseAction;
import com.dong.mcd.pub.Constants;
import com.dong.mcd.service.IRoleService;
import com.dong.mcd.service.IUserService;
import com.dong.mcd.util.CollectionUtil;
import com.dong.mcd.util.JSONUtil;
import com.dong.mcd.util.MD5Util;

@Controller
@RequestMapping("/system/user")
public class UserAction  extends BaseAction {

	@Autowired
	private IUserService userService;
	@Autowired
	private IRoleService roleService;
	
	/**
	 * 查询菜单
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/query")
	public void query(HttpServletRequest request, HttpServletResponse response,UserParam param) throws Exception {
		List<SysUser> menuList= userService.query(param);
		writeJson(response, menuList);
	}
	
	@RequestMapping("/page")
	public void queryPage(HttpServletRequest request, HttpServletResponse response,UserParam param) throws Exception {
		int pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		writeJson(response, userService.queryPage(param, pageIndex, pageSize).getPageMap());
	}
	
	@RequestMapping("/modify")
	public void modifyUser(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String json = request.getParameter("jsonData");
		try {
			UserParam user = JSONUtil.toObject(json, UserParam.class);
			if (user.getId() == null){
				writeError(response, "编号不能为空");
				return;
			}
			if (StringUtils.isEmpty(user.getLoginName())){
				writeError(response, "登录名不能为空");
				return;
			}
			userService.modify(user);
			userService.saveUserRole(user);
			writeSuccess(response, "修改成功");
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/add")
	public void add(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String json = request.getParameter("jsonData");
		try {
			UserParam user = JSONUtil.toObject(json, UserParam.class);
			if (StringUtils.isEmpty(user.getLoginName())){
				writeError(response, "登录名不能为空");
				return;
			}
			if (StringUtils.isEmpty(user.getPassword())){
				writeError(response, "密码不能为空");
				return;
			}
			UserParam param = new UserParam();
			param.setLoginName(user.getLoginName());
			if (!CollectionUtil.isEmpty(userService.query(param))){
				writeError(response, "登录名已存在");
				return;
			}
			//加密密码
			user.setPassword(MD5Util.md5Digest(user.getPassword()));
			userService.add(user);
			userService.saveUserRole(user);
			writeSuccess(response, "添加成功");
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/delete")
	public void delete(HttpServletRequest request, HttpServletResponse response,UserParam user) throws Exception{
		try {
			if (user.getId() == null){
				writeError(response, "id不能为空");
				return;
			}
			userService.remove(user.getId());
			writeSuccess(response, "删除成功");
		} catch (Exception e) {
			writeError(response, "其他错误");
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/getUser")
	public void getUser(HttpServletRequest request, HttpServletResponse response,UserParam param) throws Exception{
		writeJson(response, userService.get(param.getId()));
	}
	
	@RequestMapping("/login")
	public void login(HttpServletRequest request, HttpServletResponse response,UserParam param) throws Exception{
			try {
				if (StringUtils.isEmpty(param.getLoginName())){
					writeError(response, "用户名为空");
					return;
				}
				if (StringUtils.isEmpty(param.getPassword())){
					writeError(response, "密码为空");
					return;
				}
				SysUser user = userService.login(param.getLoginName(), param.getPassword());
				if (user == null){
					writeError(response, "登录失败，用户名或密码错误");
					return;
				}
				request.getSession().setAttribute(Constants.CURRENT_USER, user);
				writeSuccess(response, "登录成功");
			} catch (Exception e) {
				writeError(response, "登录错误");
				e.printStackTrace();
			}
	}
	
	@RequestMapping("/loginOut")
	public String loginOut(HttpServletRequest request, HttpServletResponse response) throws Exception{
			request.getSession().removeAttribute(Constants.CURRENT_USER);
			return "/web/login.jsp";
	}
	
	@RequestMapping("/toUser")
	public String toUser(HttpServletRequest request, HttpServletResponse response) throws Exception{
			RoleParam param = new RoleParam();
			request.setAttribute("roleList",roleService.query(param ));
			return "/web/system/user.jsp";
	}
	
	@RequestMapping("/getUserRole")
	public void getUserRole(HttpServletRequest request, HttpServletResponse response,UserParam param) throws Exception{
		writeJson(response, userService.getUserRoles(param.getId()));
	}
}
